<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货运单号" prop="trackNumber">
        <el-input
          v-model="queryParams.trackNumber"
          placeholder="请输入货运单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-input
          v-model="queryParams.status"
          placeholder="1=申请中2=申请通过3=已退款5=拒绝"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column label="订单号" align="center" prop="orderSn" />
      <el-table-column label="退款原因" align="center" prop="reason" />
      <el-table-column label="退款说明" align="center" prop="remark" />
      <el-table-column label="凭证" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="退款单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="货运单号" align="center" prop="trackNumber" />
      <el-table-column label="申请状态" align="center" prop="status" :formatter="refundStatus"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="subApproved(scope.row)"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="subReject(scope.row)"
          >拒绝</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="refund(scope.row)"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退款单价" prop="price">
          <el-input v-model="form.price" placeholder="" />
        </el-form-item>
        <el-form-item label="退款数量" prop="count">
          <el-input v-model="form.count" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRefund, approved, reject, queryDict, submit } from "@/api/order/refund";

export default {
  name: "Refund",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款列表表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderSn: null,
        trackNumber: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    refundStatus() {
      return (row) => {
        switch (row.status) {
          case 1:
            return '申请中';
          case 2:
            return '申请通过';
          case 3:
            return '已退款';
          case 5:
            return '拒绝';
          default:
            return '';
        }
      };
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退款列表列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderSn: null,
        reason: null,
        remark: null,
        pic: null,
        trackNumber: null,
        createTime: null,
        updateTime: null,
        status: null,
        price: null,
        count: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    subApproved(row) {
      const id = row.id || this.ids
      approved(id).then(response => {
          this.getList();
      });
    },
    subReject(row) {
      const id = row.id || this.ids
      reject(id).then(response => {
          this.getList();
      });
    },
    refund(row) {
      this.reset();
      this.form.id = row.id || this.ids;
      this.form.price = row.price;
      this.form.count = row.count;
      this.open = true;
      this.title = "退款详情";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            submit(this.form).then(response => {
              this.$modal.msgSuccess("退款成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/refund/export', {
        ...this.queryParams
      }, `refund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
